import { HallServer_SocketMsgHandler } from "../../hall_server/HallServer_SocketMsgHandler";
import { RoomModel } from "../../hall_server/model/RoomModel";
import { RoomPlayer } from "../../hall_server/model/RoomPlayer";
import { RoomTemplateModel } from "../../hall_server/model/RoomTemplateModel";
import { ServerConfig } from "../config/ServerConfig";
import { SocketClientReqManager } from "../engine/socketclient/SocketClientReqManager";
import { SocketConnectionManager } from "../engine/socketserver/socket/SocketConnectionManager";
import { HttpUtil } from "../engine/utils/HttpUtil";
import { Logger } from "../engine/utils/Logger";
import { ShellHelper } from "../engine/utils/ShellHelper";
import { GameHttpHelper } from "../game/helper/GameHttpHelper";
import { JhaoProto } from "../lib/net/protobuf/jhaoproto";
import { RoomManager } from "./RoomManager";

export class ServerManager {

    public static instance: ServerManager = new ServerManager();

    // public initServerSockets() {
    //     let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
    //     for (let i = 0; i < hall_server_list.length; i++) {
    //         let server: ServerConfig = hall_server_list[i]
    //         if (server.id != ServerConfig.nowServerConfig.id) {
    //             SocketClientReqManager.instance.req_server_login(server)
    //         }
    //     }
    // }

    public async kill_all_users() {
        let all_server_list: Array<ServerConfig> = ServerConfig.getAllSocketServer();
        for (let i = 0; i < all_server_list.length; i++) {
            let server: ServerConfig = all_server_list[i]
            SocketClientReqManager.instance.req_server_internal_kill_all_users(server)
        }
    }

    public async set_allserver_status(status: number) {
        let all_server_list: Array<ServerConfig> = ServerConfig.getAllSocketServer();
        for (let i = 0; i < all_server_list.length; i++) {
            let server: ServerConfig = all_server_list[i]
            server.status = status
            if (server.id == ServerConfig.nowServerConfig.id) {
            } else {
                SocketClientReqManager.instance.req_server_internal_set_server_status(status, server)
            }
        }
    }

    public async req_server_update_online(userids: Array<number>, server: ServerConfig) {
        let hall_server: ServerConfig = ServerConfig.randomHallServer()
        SocketClientReqManager.instance.req_server_internal_update_online(userids, server.id, server.serverType, hall_server)
    }

    private async getServerListOnlineNum(serverList: Array<ServerConfig>) {
        let onlineNum: number = 0
        Logger.log("get_all_server_online_num=serverList=", serverList)
        for (let i = 0; i < serverList.length; i++) {
            let server: ServerConfig = serverList[i]
            // let count:number = await ShellHelper.getOnline(server.socket_port)
            // onlineNum += count
            let response = await GameHttpHelper.get_online_num(server.getInternalUrl() + "/online")
            if (response["count"]) {
                onlineNum += response["count"];
            }
        }
        return onlineNum
    }

    public async get_all_server_online_num() {
        let onlineNum: number = 0
        let server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        // onlineNum += await this.getServerListOnlineNum(server_list)
        // server_list = ServerConfig.huanghua_mj_server_list
        // onlineNum += await this.getServerListOnlineNum(server_list)
        // server_list = ServerConfig.ddz_server_list
        // onlineNum += await this.getServerListOnlineNum(server_list)
        // server_list = ServerConfig.jianziding_server_list
        // onlineNum += await this.getServerListOnlineNum(server_list)
        // server_list = ServerConfig.cangxian_mj_server_list
        // onlineNum += await this.getServerListOnlineNum(server_list)
        // server_list = ServerConfig.paodekuai_server_list
        // onlineNum += await this.getServerListOnlineNum(server_list)
        // server_list = ServerConfig.dazhonger_mj_server_list
        // onlineNum += await this.getServerListOnlineNum(server_list)
        return onlineNum
    }

    public async get_all_hallserver_online_num() {
        let onlineNum: number = 0
        let server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        onlineNum += await this.getServerListOnlineNum(server_list)
        return onlineNum
    }


    public async is_in_server(account: string) {
        let resultServerConfig: ServerConfig = null
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            let isIn: boolean = await SocketClientReqManager.instance.req_server_internal_is_in_server(account, server)
            if (isIn) {
                resultServerConfig = server;
                return resultServerConfig;
            }
        }
        return resultServerConfig
    }

    public async is_user_online(userid: number) {
        let isOnline: boolean = false
        let all_server_list: Array<ServerConfig> = ServerConfig.getAllSocketServer();
        for (let i = 0; i < all_server_list.length; i++) {
            let server: ServerConfig = all_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
                isOnline = SocketConnectionManager.instance.isUserOnline(userid)
                if (isOnline) {
                    break;
                }
            } else {
                isOnline = await SocketClientReqManager.instance.req_server_internal_is_user_online(userid, server)
                if (isOnline) {
                    break;
                }
            }
        }

        return isOnline
    }

    private async getServerListClubOnlineNum(server_list: Array<ServerConfig>, clubid: number) {
        let onlineNum: number = 0
        let count: number = 0
        for (let i = 0; i < server_list.length; i++) {
            let server: ServerConfig = server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
                count = SocketConnectionManager.instance.getClubOnlineCount(clubid)
            } else {
                count = 0
                // let response = await SocketClientReqManager.instance.req_server_onlie_club(clubid, server)
                // if (response && response["count"]) {
                //     count = response["count"]
                // }
            }
            onlineNum += count;
        }
        return onlineNum
    }

    public async getClubOnlineNum(clubid: number) {
        let server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        let onlineNum: number = await this.getServerListClubOnlineNum(server_list, clubid)
        Logger.log("getClubOnlineNum===", clubid, onlineNum)
        return onlineNum;
    }

    public async notify_delete_club_user(clubid: number, userid: number) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
                // HallServer_SocketMsgHandler.notify_delete_club_user(clubid, userid)
            } else {
                SocketClientReqManager.instance.req_server_notify_delete_club_user(clubid, userid, server)
            }
        }
    }

    public async notify_set_club_quanxian(clubid: number, userid: number, quanxian: string) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
                // HallServer_SocketMsgHandler.notify_set_club_quanxian(clubid, userid, quanxian)
            } else {
                SocketClientReqManager.instance.req_server_notify_set_club_quanxian(clubid, userid, quanxian, server)
            }
        }
    }

    public async notify_club_freeze(clubid: number, userid: number, user_state: number) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
                // HallServer_SocketMsgHandler.notify_club_freeze(clubid, userid, user_state)
            } else {
                SocketClientReqManager.instance.req_server_notify_club_freeze(clubid, userid, user_state, server)
            }
        }
    }

    // public async notify_set_club_admin(clubid: number, userid: number, actor: number, quanxian: string) {
    //     let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
    //     for (let i = 0; i < hall_server_list.length; i++) {
    //         let server: ServerConfig = hall_server_list[i]
    //         if (server.id == ServerConfig.nowServerConfig.id) {
    //             HallServer_SocketMsgHandler.notify_set_club_admin(clubid, userid, actor, quanxian)
    //         } else {
    //             SocketClientReqManager.instance.req_server_notify_set_club_admin(clubid, userid, actor, quanxian, server)
    //         }
    //     }
    // }

    public async insertRoomTemplateModel() {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
                RoomTemplateModel.init()
            } else {
                SocketClientReqManager.instance.req_server_insertRoomTemplateModel(server)
            }
        }
    }

    public async deleteRoomTemplate(id: number) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
                RoomTemplateModel.deleteRoomTemplate(id)
            } else {
                SocketClientReqManager.instance.req_server_internal_club_delete_room_template(id, server)
            }
        }
    }

    public async createRoom(room: JhaoProto.IRoomModel) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
                RoomModel.roomMap.set(room.roomId, room)
            } else {
                SocketClientReqManager.instance.req_server_internal_createRoom(room, server)
            }
        }
        let serverConfig: ServerConfig = ServerConfig.getGameServerById(room.serverId)
        if (serverConfig) {
            SocketClientReqManager.instance.req_server_internal_createRoom(room, serverConfig)
        } else {
            Logger.warn("找不到服务器id", room)
        }
    }

    public async joinRoom(room: RoomModel, player: RoomPlayer) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
                // RoomModel.roomMap.set(room.room_id, room)
            } else {
                SocketClientReqManager.instance.req_server_joinRoom(room.room_id, player, server)
            }
        }
        let serverConfig: ServerConfig = ServerConfig.getGameServerById(room.server_id)
        if (serverConfig) {
            SocketClientReqManager.instance.req_server_joinRoom(room.room_id, player, serverConfig)
        } else {
            Logger.warn("找不到服务器id", room)
        }
    }

    public async leaveRoom(room: RoomModel) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            SocketClientReqManager.instance.req_server_leave_room(room, server)
        }
    }

    public async deleteRoom(room_id: number) {
        RoomModel.roomMap.delete(room_id)
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            SocketClientReqManager.instance.req_server_deleteRoom(room_id, server)
        }
    }

    public notify_game_start(room: RoomModel) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            SocketClientReqManager.instance.req_server_notify_game_start(room, server)
        }
    }


    public notify_update_gems(userid: number) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
                HallServer_SocketMsgHandler.notify_update_gems(userid)
            } else {
                SocketClientReqManager.instance.req_server_notify_update_gems(userid, server)
            }
        }
    }

    public notify_update_coins(userid: number) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
                HallServer_SocketMsgHandler.notify_update_coins(userid)
            } else {
                SocketClientReqManager.instance.req_server_notify_update_coins(userid, server)
            }
        }
        let gameServerList: Array<ServerConfig> = ServerConfig.getAllGameServer();
        for (let i = 0; i < gameServerList.length; i++) {
            let server: ServerConfig = gameServerList[i]
            SocketClientReqManager.instance.req_server_notify_update_coins(userid, server)
        }
    }

    public notify_update_income(userid: number) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
            } else {
                SocketClientReqManager.instance.req_server_notify_update_coins(userid, server)
            }
        }
    }

    public notify_update_meili(userid: number) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
            } else {
                SocketClientReqManager.instance.req_server_notify_update_meili(userid, server)
            }
        }
    }

    public notify_update_lv(userid: number) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
            } else {
                SocketClientReqManager.instance.req_server_notify_update_lv(userid, server)
            }
        }
    }

    public notify_update_invite(userid: number) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
            } else {
                SocketClientReqManager.instance.req_server_notify_update_invite(userid, server)
            }
        }
    }

    public notify_update_img(userid: number, imgurl: string, upload_pic_type: number) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            SocketClientReqManager.instance.req_server_notify_update_img(userid, imgurl, upload_pic_type, server)
        }
    }


    public notify_club_open_or_close(clubid: number, state: number) {
        let hall_server_list: Array<ServerConfig> = ServerConfig.hall_server_List;
        for (let i = 0; i < hall_server_list.length; i++) {
            let server: ServerConfig = hall_server_list[i]
            if (server.id == ServerConfig.nowServerConfig.id) {
                SocketConnectionManager.instance.notify_club_open_or_close(clubid, state)
            } else {
                SocketClientReqManager.instance.req_server_notify_club_open_or_close(clubid, state, server)
            }
        }
    }

    public delete_room_by_hall(room_id: number, server: ServerConfig, nick_name: string) {
        SocketClientReqManager.instance.req_server_delete_room_by_hall(room_id, nick_name, server)
    }
}